package com.ruoyi.cderp.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.TblPreTakeStockSheetDetail;
import com.ruoyi.cderp.mapper.TblPreTakeStockSheetMapper;
import com.ruoyi.cderp.domain.TblPreTakeStockSheet;
import com.ruoyi.cderp.service.ITblPreTakeStockSheetService;

/**
 * 库存预先盘点单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@Service
public class TblPreTakeStockSheetServiceImpl implements ITblPreTakeStockSheetService 
{
    @Autowired
    private TblPreTakeStockSheetMapper tblPreTakeStockSheetMapper;

    /**
     * 查询库存预先盘点单
     * 
     * @param id 库存预先盘点单主键
     * @return 库存预先盘点单
     */
    @Override
    public TblPreTakeStockSheet selectTblPreTakeStockSheetById(String id)
    {
        return tblPreTakeStockSheetMapper.selectTblPreTakeStockSheetById(id);
    }

    /**
     * 查询库存预先盘点单列表
     * 
     * @param tblPreTakeStockSheet 库存预先盘点单
     * @return 库存预先盘点单
     */
    @Override
    public List<TblPreTakeStockSheet> selectTblPreTakeStockSheetList(TblPreTakeStockSheet tblPreTakeStockSheet)
    {
        return tblPreTakeStockSheetMapper.selectTblPreTakeStockSheetList(tblPreTakeStockSheet);
    }

    /**
     * 新增库存预先盘点单
     * 
     * @param tblPreTakeStockSheet 库存预先盘点单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTblPreTakeStockSheet(TblPreTakeStockSheet tblPreTakeStockSheet)
    {
        tblPreTakeStockSheet.setCreateTime(DateUtils.getNowDate());
        int rows = tblPreTakeStockSheetMapper.insertTblPreTakeStockSheet(tblPreTakeStockSheet);
        insertTblPreTakeStockSheetDetail(tblPreTakeStockSheet);
        return rows;
    }

    /**
     * 修改库存预先盘点单
     * 
     * @param tblPreTakeStockSheet 库存预先盘点单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTblPreTakeStockSheet(TblPreTakeStockSheet tblPreTakeStockSheet)
    {
        tblPreTakeStockSheet.setUpdateTime(DateUtils.getNowDate());
        tblPreTakeStockSheetMapper.deleteTblPreTakeStockSheetDetailBySheetId(tblPreTakeStockSheet.getId());
        insertTblPreTakeStockSheetDetail(tblPreTakeStockSheet);
        return tblPreTakeStockSheetMapper.updateTblPreTakeStockSheet(tblPreTakeStockSheet);
    }

    /**
     * 批量删除库存预先盘点单
     * 
     * @param ids 需要删除的库存预先盘点单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblPreTakeStockSheetByIds(String[] ids)
    {
        tblPreTakeStockSheetMapper.deleteTblPreTakeStockSheetDetailBySheetIds(ids);
        return tblPreTakeStockSheetMapper.deleteTblPreTakeStockSheetByIds(ids);
    }

    /**
     * 删除库存预先盘点单信息
     * 
     * @param id 库存预先盘点单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblPreTakeStockSheetById(String id)
    {
        tblPreTakeStockSheetMapper.deleteTblPreTakeStockSheetDetailBySheetId(id);
        return tblPreTakeStockSheetMapper.deleteTblPreTakeStockSheetById(id);
    }

    /**
     * 新增库存预先盘点单明细信息
     * 
     * @param tblPreTakeStockSheet 库存预先盘点单对象
     */
    public void insertTblPreTakeStockSheetDetail(TblPreTakeStockSheet tblPreTakeStockSheet)
    {
        List<TblPreTakeStockSheetDetail> tblPreTakeStockSheetDetailList = tblPreTakeStockSheet.getTblPreTakeStockSheetDetailList();
        String id = tblPreTakeStockSheet.getId();
        if (StringUtils.isNotNull(tblPreTakeStockSheetDetailList))
        {
            List<TblPreTakeStockSheetDetail> list = new ArrayList<TblPreTakeStockSheetDetail>();
            for (TblPreTakeStockSheetDetail tblPreTakeStockSheetDetail : tblPreTakeStockSheetDetailList)
            {
                tblPreTakeStockSheetDetail.setSheetId(id);
                list.add(tblPreTakeStockSheetDetail);
            }
            if (list.size() > 0)
            {
                tblPreTakeStockSheetMapper.batchTblPreTakeStockSheetDetail(list);
            }
        }
    }
}
